package vip

import (
	"context"
	"github.com/beherochuling/beesoft_api/app/helper/net"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/beherochuling/beesoft_api/app/dao"
	"github.com/beherochuling/beesoft_api/app/model"
	"github.com/beherochuling/beesoft_api/app/model/entity"
	"github.com/beherochuling/beesoft_api/app/model/vip"
)

var Activity activity

type activity struct{}

func (*activity) Scan(ctx context.Context, where g.Map) *entity.VipActivities {
	var item *entity.VipActivities

	err := dao.VipActivities.Ctx(ctx).
		Where(where).
		Scan(&item)
	if err != nil {
		panic(err)
	}

	return item
}
func (r *activity) ScanBySlug(ctx context.Context, slug string) *entity.VipActivities {
	var item *entity.VipActivities

	err := dao.VipActivities.Ctx(ctx).
		Where(dao.VipActivities.Columns().Slug, slug).
		Scan(&item)
	if err != nil {
		panic(err)
	}

	return item
}
func (*activity) Scans(ctx context.Context, where g.Map, page model.Pagination) []entity.VipActivities {
	var activities []entity.VipActivities

	m := dao.VipActivities.Ctx(ctx).
		Where(where)

	offset := (page.Page - 1) * page.PerPage
	m = m.Offset(offset).
		Limit(page.PerPage)

	err := m.OrderDesc(dao.VipActivities.Columns().Id).
		Scan(&activities)
	if err != nil {
		panic(err)
	}

	return activities
}
func (*activity) Count(ctx context.Context, where g.Map) int {
	count, err := dao.VipActivities.Ctx(ctx).
		Where(where).
		Count()
	if err != nil {
		panic(err)
	}

	return count
}

func (r *activity) List(ctx context.Context, req *vip.ActivityListRequest) (int, []entity.VipActivities) {
	where := g.Map{}
	if req.Id != 0 {
		where[dao.VipActivities.Columns().Id] = req.Id
	}
	if req.Slug != "" {
		where[dao.VipActivities.Columns().Slug] = req.Slug
	}
	if req.Title != "" {
		where[dao.VipActivities.Columns().Title] = req.Title
	}

	count := r.Count(ctx, where)
	var items []entity.VipActivities
	if count > 0 {
		items = r.Scans(ctx, where, req.Pagination)
	}

	return count, items
}
func (r *activity) Show(ctx context.Context, slug string) (net.ErrorCode, *entity.VipActivities) {
	item := r.ScanBySlug(ctx, slug)
	if item == nil {
		return model.ErrorCodeNotFound, nil
	}

	return model.SuccessCode, item
}
func (r *activity) Attend(ctx context.Context, req *vip.ActivityAttendRequest) net.ErrorCode {
	return model.SuccessCode
}
func (r *activity) Store() net.ErrorCode {
	return model.SuccessCode
}
func (r *activity) Update() net.ErrorCode {
	return model.SuccessCode
}
